home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / ir / wmessage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  4.0 KB  |  162 lines

  1. /* WIDE AREA INFORMATION SERVER SOFTWARE
  2.    No guarantees or restrictions.  See the readme file for the full standard
  3.    disclaimer.    
  4.    3.26.90
  5. */
  6.  
  7. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  8.  
  9.  
  10. /* This file is for reading and writing the wais packet header.
  11.  * Morris@think.com
  12.  */
  13.  
  14. #ifndef lint
  15. static char *RCSid = "$Header: /usr/local/ls6/src+data/src/freeWAIS-sf/ir/RCS/wmessage.c,v 1.4 1994/08/12 17:47:54 pfeifer Exp $";
  16. #endif
  17.  
  18. /* Change log:
  19.  * $Log: wmessage.c,v $
  20.  * Revision 1.4  1994/08/12  17:47:54  pfeifer
  21.  * in writeWAISPacketHeader replaced call to strlen because it made
  22.  * purify unhappy
  23.  *
  24.  * Revision 1.3  1994/05/20  12:58:04  pfeifer
  25.  * beta
  26.  *
  27.  * Revision 1.2  1994/03/08  21:07:13  pfeifer
  28.  * Patchlevel 04
  29.  *
  30.  * Revision 1.1  1993/02/16  15:05:35  freewais
  31.  * Initial revision
  32.  *
  33.  * Revision 1.2  92/02/12  13:56:35  jonathan
  34.  * Added "$Log" so RCS will put the log message in the header
  35.  * 
  36.  * 
  37. */
  38.  
  39. /* to do:
  40.  *  add check sum
  41.  *  what do you do when checksum is wrong?
  42.  */
  43.  
  44. /* #include <string.h> */
  45. #include "wmessage.h"
  46. #include "ustubs.h"
  47. #include "cutil.h"
  48.  
  49. /*---------------------------------------------------------------------*/
  50.  
  51. void 
  52. readWAISPacketHeader(msgBuffer,header_struct)
  53. char* msgBuffer;
  54. WAISMessage *header_struct;
  55. {
  56.   /* msgBuffer is a string containing at least HEADER_LENGTH bytes. */
  57.             
  58.   memmove(header_struct->msg_len,msgBuffer,(size_t)10); 
  59.   header_struct->msg_type = char_downcase((unsigned long)msgBuffer[10]);
  60.   header_struct->hdr_vers = char_downcase((unsigned long)msgBuffer[11]);
  61.   memmove(header_struct->server,(void*)(msgBuffer + 12),(size_t)10);
  62.   header_struct->compression = char_downcase((unsigned long)msgBuffer[22]);
  63.   header_struct->encoding = char_downcase((unsigned long)msgBuffer[23]);
  64.   header_struct->msg_checksum = char_downcase((unsigned long)msgBuffer[24]);
  65. }
  66.  
  67. /*---------------------------------------------------------------------*/
  68.  
  69. long
  70. getWAISPacketLength(header)
  71. WAISMessage* header;
  72. /* interpret the length field, this is necessary since the lenght in the
  73.    message is not null terminated, so atol() may get confused.
  74.  */
  75.   char lenBuf[11];
  76.   memmove(lenBuf,header->msg_len,(size_t)10);
  77.   lenBuf[10] = '\0';
  78.   return(atol(lenBuf));
  79. }
  80.  
  81. /*---------------------------------------------------------------------*/
  82.  
  83. #ifdef NOTUSEDYET
  84.  
  85. static char checkSum _AP((char* string,long len));
  86.  
  87. static char
  88. checkSum(string,len)
  89. char* string;
  90. long len;
  91. /* XXX the problem with this routine is that it can generate 
  92.    non-ascii values.  Since these values are not being hexized,
  93.    they can (and will) hang up some communication channels.
  94.    */
  95. {
  96.   register long i;
  97.   register char chSum = '\0';
  98.       
  99.   for (i = 0; i < len; i++)
  100.     chSum = chSum ^ string[i];
  101.         
  102.   return(chSum);
  103. }    
  104. #endif /* def NOTUSEDYET */
  105.  
  106. /* this modifies the header argument.  See wais-message.h for the different
  107.  * options for the arguments.
  108.  */
  109.  
  110. void
  111. writeWAISPacketHeader(header,
  112.               dataLen,
  113.               type,
  114.               server,
  115.               compression,
  116.               encoding,
  117.               version)
  118. char* header;
  119. long dataLen;
  120. long type;
  121. char* server;
  122. long compression;
  123. long encoding;
  124. long version;
  125. /* Puts together the new wais before-the-z39-packet header. */
  126. {
  127.   char lengthBuf[11];
  128.   char serverBuf[11];
  129.  
  130.   long serverLen;
  131.  
  132.   /* 
  133.      replace the strlen; which reads until it finds '\0' and make
  134.      purify happy. Think time is nor problem. (up)
  135.  
  136.      serverLen = strlen(server);
  137.      if (serverLen > 10)
  138.      serverLen = 10;
  139.   */
  140.   for (serverLen=0;serverLen<=10 && server[serverLen]; serverLen++);
  141.  
  142.   sprintf(lengthBuf, "%010ld", dataLen);  
  143.   strncpy(header,lengthBuf,10);
  144.  
  145.   header[10] = type & 0xFF; 
  146.   header[11] = version & 0xFF;
  147.  
  148.   strncpy(serverBuf,server,serverLen);       
  149.   strncpy((char*)(header + 12),serverBuf,serverLen);
  150.  
  151.   header[22] = compression & 0xFF;    
  152.   header[23] = encoding & 0xFF;    
  153.   header[24] = '0'; /* checkSum(header + HEADER_LENGTH,dataLen);   XXX the result must be ascii */    
  154. }              
  155.               
  156. /*---------------------------------------------------------------------*/
  157.  
  158.  
  159.  
  160.  
  161.